package com.sunyard.demo.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.limiter.RedisTokenBucketRateLimiter;
import sun.plugin.util.UserProfile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author zyl
 * @description
 * @since 2025/8/22
 */
@RestController
@RequestMapping("/api")
public class ApiController {

    @Resource
    private RedisTokenBucketRateLimiter apiRateLimiter;

    @PostMapping("/user/profile")
    public ResponseEntity<?> updateUserProfile(@RequestBody UserProfile profile,
                                               HttpServletRequest request) {
//        String userId = getUserIdFromRequest(request);

        if (!apiRateLimiter.tryAcquire()) {
            return ResponseEntity.status(429)
                    .body("请求过于频繁");
        }

        // 正常业务处理
//        userService.updateProfile(userId, profile);
        return ResponseEntity.ok("更新成功");
    }
}
